package kbl.language;

import java.util.ArrayList;

class RegularExpression {
	private ArrayList<RElement> postfix = new ArrayList<RElement>();
	private String name;

	RegularExpression(String name) {
		this.name = name;
	}

	public void add(RElement re) {
		postfix.add(re);
	}

	public ArrayList<RElement> getListRe() {
		return postfix;
	}

	public String getName() {
		return name;
	}

	@Override
	public String toString() {
		StringBuffer sb = new StringBuffer();
		for (RElement re : postfix){
			sb.append(re.toString());
		}
		return sb.toString();		
	}
}

class RElement {
	private CommonType op;
	private String name;
	private char c;
	private CharacterClass cc;

	public RElement(char c) {
		switch (c) {
		case '(':
			this.op = CommonType.LParenthesis;
			break;
		case ')':
			this.op = CommonType.RParenthesis;
			break;
		case '[':
			this.op = CommonType.LBracket;
			break;
		case ']':
			this.op = CommonType.RBracket;
			break;
		case '*':
			this.op = CommonType.Asterisk;
			break;
		case '+':
			this.op = CommonType.Plus;
			break;
		case '|':
			this.op = CommonType.Alternate;
			break;
		case '?':
			this.op = CommonType.QuestionMark;
			break;
		case '.':
			this.op = CommonType.CharacterClass;
			break;
		case '\\':
			this.op = CommonType.Escape;
			break;
		default:
			this.op = CommonType.Literal;
			break;
		}
		this.c = c;
		this.name = Character.toString(c);
	}

	public RElement(CharacterClass cc) {
		this.op = CommonType.CharacterClass;
		this.cc = cc;
	}

	public RElement(String string) {
		// TODO Auto-generated constructor stub
	}

	public RElement(CommonType ct, String name) {
		this(ct);
		this.name = name;
	}

	public RElement(CommonType ct, char c) {
		this(ct);
		this.c = c;
		this.name = Character.toString(c);
	}

	public RElement(CommonType ct) {
		switch (ct) {
		case LParenthesis:
			this.c = '(';
			break;
		case RParenthesis:
			this.c = ')';
			break;
		case LBracket:
			this.c = '[';
			break;
		case RBracket:
			this.c = ']';
			break;
		case Asterisk:
			this.c = '*';
			break;
		case Plus:
			this.c = '+';
			break;
		case Alternate:
			this.c = '|';
			break;
		case QuestionMark:
			this.c = '?';
			break;
		case CharacterClass:
			this.c = '.';
			break;
		case Escape:
			this.c = '\\';
			break;
		case Concatenate:
			this.c = '&';
			break;
		default:
			this.c = '$';
			break;
		}
		this.op = ct;
		this.name = Character.toString(c);
	}

	public CommonType getOperator() {
		return this.op;
	}

	public boolean contains(char c) {
		return this.c == c;
	}

	public CharacterClass getCC() {
		return cc;
	}

	public char getLiteral() {
		return c;
	}

	@Override
	public String toString() {
		if (this.op == CommonType.CharacterClass) {
			return cc.toString();
		} else {
			return this.name;
		}
	}
}
